close all;
clear;clc;
Lower_Switch = 1;
Puter_Name = getenv('computername');
if strcmp(Puter_Name, 'CALCENTER2')
    Puter_Name_Pre = 'D:\vc\mail.ustc.edu.cn\Rb87BEC - Files\';
elseif strcmp(Puter_Name, 'VICTORCHENGPC')
    Puter_Name_Pre = 'X:\Work\mail.ustc.edu.cn\Rb87BEC - Files\';
end
Row_Start = 202;
mz_simed = -0.25;
if mz_simed < 0
    shift = 2;
else
    shift = 0;
end
UpDown_Shift = 3;
load(sprintf("%s\\Rb87Data\\3DSOCbandData\\190827\\ky_detuning_pair,mz_sim=%.3f.mat", Puter_Name_Pre, mz_simed))
if Row_Start == 119
    ky_detuning_pair(9:21,:) = [];
end
if Row_Start == 98 || 202
    ky_detuning_pair = flip(ky_detuning_pair);
end
detuning_measure_list = ky_detuning_pair(:, 2);
qy_measure_list = ky_detuning_pair(:, 1);
% detuning_measure_list = [0.03 0.025 detuning_measure_list];
% qy_measure_list = [0.05 0 qy_measure_list];

Z_Spacing = 1.3;
clims = [-0.5, 0.5];

if strcmp(Puter_Name, 'CALCENTER2')
    load("D:\vc\Rb87Python\colormap_symmetric.mat");
elseif strcmp(Puter_Name, 'VICTORCHENGPC')
%     load("X:\work\Rb87Python\colormap_thinwhite.mat");
    load("X:\work\Rb87Python\colormap_symmetric.mat");
end

if mz_simed < 0
    M_Center_Switch = 1;
else
    M_Center_Switch = 0;
end
if M_Center_Switch == 1
    Center_Point = 'M';
else
    Center_Point = 'Gamma';
end

% Data_Excel = "D:\Rb87files\3DSOC_spin_texture\spin_texture.xlsx";
% % Data_Excel = "spin_texture.xlsx";
% Select_Matrix = readcell(Data_Excel, 'Sheet', 'Meta');
% Select_Row_List = 77:98;


figure(1)

z_ticks = [];
z_label = {};
for detuning_measure_index = 1:length(detuning_measure_list)
    detuning_measure = detuning_measure_list(detuning_measure_index);
    qy_measure = qy_measure_list(detuning_measure_index);
    qy_measure_ori = qy_measure_list(detuning_measure_index);
    if Row_Start == 98 || 202
        if (qy_measure < 0.5 && qy_measure ~= 0 && Lower_Switch == 0)% || mod(detuning_measure_index, 2) == 1
            continue
        end
        if Lower_Switch == 1 && mod(detuning_measure_index, 2) == 0
            continue
        end
%         if mod(detuning_measure_index, 2) == 0
%             continue
%         end
        if qy_measure == 0 && Lower_Switch == 0
            qy_measure = 0.5-0.05*3;
        end
    end
    Row = Row_Start+detuning_measure_index-1;
    if Lower_Switch == 1
      load(sprintf("%s\\Rb87Data\\3DSOC_spin_texture\\Selected_saved\\ClassifyMeta_Row=%d,Detuning=%.3f_Lower.mat", Puter_Name_Pre, Row, detuning_measure), 'SP_Lower_Ave')
      SP_Ave = SP_Lower_Ave;
    else
      load(sprintf("%s\\Rb87Data\\3DSOC_spin_texture\\Selected_saved\\ClassifyMeta_Row=%d,Detuning=%.3f.mat", Puter_Name_Pre, Row, detuning_measure), 'SP_Ave')
    end
    FBZ = size(SP_Ave);
    if FBZ(1) == 1
        continue
    end
    if M_Center_Switch == 1
        SP_Ave = [SP_Ave(ceil(FBZ(1)/2):end-1,:);SP_Ave(1:ceil(FBZ(1)/2),:)];
        SP_Ave = [SP_Ave(:, ceil(FBZ(2)/2):end-1),SP_Ave(:,1:ceil(FBZ(2)/2))];
    end
    ku_list = linspace(-1, 1, FBZ(1));
    kv_list = linspace(-1, 1, FBZ(2));
%     if M_Center_Switch == 1
%         ku_list = ku_list + 1;
%         kv_list = kv_list + 1;
%     end
    [Y, X] = meshgrid(kv_list, ku_list);
    Z = Z_Spacing*(zeros(size(Y))+qy_measure);
    z_ticks(length(z_ticks)+1) = Z(1,1);
%     z_label{length(z_label)+1}=sprintf("mz_PWM=%.3fEr, ky=%.3fpi", detuning_measure/2, qy_measure);
    z_label{length(z_label)+1}=sprintf("%.3fpi", qy_measure_ori);
    hold on;
    h = surf(X, Y, Z, SP_Ave);
    set(h,'edgecolor','none','lineStyle','none');
    material dull
    axis equal
    colormap(cmap);
    caxis(clims)
%     text(1.5,1.5,Z_Spacing*ky_simed,sprintf("detuning=%.3fEr, ky=%.3fpi",detuning, ky_simed/pi));
    pause(0.01)
end

for detuning_measure_index = 1:length(detuning_measure_list)
    detuning_measure = detuning_measure_list(detuning_measure_index);
    qy_measure = -qy_measure_list(detuning_measure_index)+shift;
    qy_measure_ori = -qy_measure_list(detuning_measure_index)+shift;
    if Row_Start == 98 || 202
        if (qy_measure > 1.5 && qy_measure ~= 2 && Lower_Switch == 0)% || mod(detuning_measure_index, 2) == 1
            continue
        end
        if Lower_Switch == 1 && mod(detuning_measure_index, 2) == 0
            continue
        end
        if qy_measure == 2 && Lower_Switch == 0
            qy_measure = 1.5+0.05*3;
        end
%         if mod(detuning_measure_index, 2) == 0
%             continue
%         end
    end
    if qy_measure_list(detuning_measure_index) == 100
        continue
    end
    Row = Row_Start+detuning_measure_index-1;
    if Lower_Switch == 1
      load(sprintf("%s\\Rb87Data\\3DSOC_spin_texture\\Selected_saved\\ClassifyMeta_Row=%d,Detuning=%.3f_Lower.mat", Puter_Name_Pre, Row, detuning_measure), 'SP_Lower_Ave')
      SP_Ave = SP_Lower_Ave;
    else
      load(sprintf("%s\\Rb87Data\\3DSOC_spin_texture\\Selected_saved\\ClassifyMeta_Row=%d,Detuning=%.3f.mat", Puter_Name_Pre, Row, detuning_measure), 'SP_Ave')
    end
    FBZ = size(SP_Ave);
    if FBZ(1) == 1
        continue
    end
    if M_Center_Switch == 1
        SP_Ave = [SP_Ave(ceil(FBZ(1)/2):end-1,:);SP_Ave(1:ceil(FBZ(1)/2),:)];
        SP_Ave = [SP_Ave(:, ceil(FBZ(2)/2):end-1),SP_Ave(:,1:ceil(FBZ(2)/2))];
    end
    ku_list = linspace(-1, 1, FBZ(1));
    kv_list = linspace(-1, 1, FBZ(2));
%     if M_Center_Switch == 1
%         ku_list = ku_list + 1;
%         kv_list = kv_list + 1;
%     end
    [Y, X] = meshgrid(kv_list, ku_list);
    Z = Z_Spacing*(zeros(size(Y))+qy_measure);
    z_ticks(length(z_ticks)+1) = Z(1,1);
%     z_label{length(z_label)+1}=sprintf("mz_PWM=%.3fEr, ky=%.3fpi", detuning_measure/2, qy_measure);
    z_label{length(z_label)+1}=sprintf("%.3fpi", qy_measure_ori);
    hold on;
    h = surf(X, Y, Z, SP_Ave);
    set(h,'edgecolor','none','lineStyle','none');
    material dull
    axis equal
    colormap(cmap);
    caxis(clims)
%     text(1.5,1.5,Z_Spacing*ky_simed,sprintf("detuning=%.3fEr, ky=%.3fpi",detuning, ky_simed/pi));
    pause(0.01)
end

[z_ticks,ia,~] = unique(z_ticks);
z_labels = z_label(ia);
zticks(z_ticks);
zticklabels(z_labels)

axis tight
view(30,2.5);
if Lower_Switch == 0
    z_ticks = [0.5-0.05*UpDown_Shift 0.5:0.1:1.5 1.5+0.05*UpDown_Shift] * Z_Spacing;
    te = [0 0.5:0.1:1.5 2]-1;
else
    z_ticks = [0:0.2:2] * Z_Spacing;
    te = [0:0.2:2]-1;
end
zticks(z_ticks);
zticklabel=strings(1,length(zticks));
for i=1:length(zticks)
    zticklabel(i)=sprintf('$%0.1f$',te(i));
end
zticklabels(zticklabel)
title('Correction');
set(gca,'PlotBoxAspectRatioMode','Auto',...
'DataAspectRatioMode','Auto',...
'YTickLabel',{'' '$0$' '$1$'},...
'XTickLabel',{'$1$' '$0$' '$-1$'},...
'Clim',clims,...
'FontName','Times New Roman',...
'FontSize',35,...
'LineWidth',3)

xlh = xlabel('$q_{u}/\pi$','FontName','Times New Roman');%,'Position',[1.2,0,0.8])
ylh = ylabel('$q_{v}/\pi$','FontName','Times New Roman');%,'Position',[2,1.2,0.8])
zlh = zlabel('$q_{z}/\pi$','FontName','Times New Roman');
xlh.Interpreter = 'latex';
ylh.Interpreter = 'latex';
zlh.Interpreter = 'latex';
xlh.Position = [0.25, -1, 0.31];
ylh.Position = [1, -0.25, 0.31];
if Lower_Switch == 1
    xlh.Position = [0.25, -1, -0.2];
    ylh.Position = [1, -0.25, -0.2];
end
set(gcf,'position',[-1000.6667  -100.3333  624*1.7  800*1.7]);
set(gca,'OuterPosition',[0.05 0.05 0.8 0.8])

% set(gcf,'Units','Centimeters',...
%    'PaperPosition',[14.0075 0.6345 22.7850 22.8610],'PaperSize',[50.8,24.13]);
% colorbar
% if strcmp(Puter_Name, 'CALCENTER2')
%     savefig(gcf, ['D:\Rb87figures\3DSOC_spin_texture\figures\Stack_New\Stack' 'V0=-1.770,Vy=-3.540,Oxy=1.022,mz_simed=' num2str(mz_simed) Center_Point '_Center' 'Experiment_Stack_rowstart' num2str(Row_Start) '.fig'], 'compact')
%     saveas(gcf, ['D:\Rb87figures\3DSOC_spin_texture\figures\Stack_New\Stack' 'V0=-1.770,Vy=-3.540,Oxy=1.022,mz_simed=' num2str(mz_simed) Center_Point '_Center' 'Experiment_Stack_rowstart' num2str(Row_Start) '.png'])
% end
figh = gcf;
ax = gca;
ax.XTickMode = 'manual';
ax.YTickMode = 'manual';
ax.ZTickMode = 'manual';
ax.XLimMode = 'manual';
ax.YLimMode = 'manual';
ax.ZLimMode = 'manual';
ax.TickLabelInterpreter = 'latex';
set(gcf,'Units','Centimeters');
figh.PaperPosition = [0,0,figh.Position(3:4)];
figh.PaperSize = figh.Position(3:4);
figh.Renderer = 'Painters';
print('C:\Users\cheng\Desktop\StackExp05_25','-dpdf','-r0')

figure(2)
% cmap(32:35,:) = 0;
for detuning_measure_index = 1:length(detuning_measure_list)
    detuning_measure = detuning_measure_list(detuning_measure_index);
    qy_measure = qy_measure_list(detuning_measure_index);
    Row = Row_Start+detuning_measure_index-1;
    if Lower_Switch == 1
      load(sprintf("%s\\Rb87Data\\3DSOC_spin_texture\\Selected_saved\\ClassifyMeta_Row=%d,Detuning=%.3f_Lower.mat", Puter_Name_Pre, Row, detuning_measure), 'SP_Lower_Ave')
      SP_Ave = SP_Lower_Ave;
    else
      load(sprintf("%s\\Rb87Data\\3DSOC_spin_texture\\Selected_saved\\ClassifyMeta_Row=%d,Detuning=%.3f.mat", Puter_Name_Pre, Row, detuning_measure), 'SP_Ave')
    end
    FBZ = size(SP_Ave);
    if FBZ(1) == 1
        continue
    end
    if M_Center_Switch == 1
        SP_Ave = [SP_Ave(ceil(FBZ(1)/2):end-1,:);SP_Ave(1:ceil(FBZ(1)/2),:)];
        SP_Ave = [SP_Ave(:, ceil(FBZ(2)/2):end-1),SP_Ave(:,1:ceil(FBZ(2)/2))];
    end
    subplot(5, 5, detuning_measure_index);
    imagesc(SP_Ave, clims)
%     tth = title(['detuning=' num2str(detuning_measure)]);
    
    axis equal
    axis off
    colormap(cmap);
    if strcmp(Puter_Name, 'CALCENTER2')
        savefig(gcf, ['C:\Users\cheng\Desktop\Sing05_1' 'V0=-1.770,Vy=-3.540,Oxy=1.022,mz_simed=' num2str(mz_simed) Center_Point '_Center' 'Experiment_Flat_rowstart' num2str(Row_Start) '.fig'], 'compact')
        saveas(gcf, ['C:\Users\cheng\Desktop\Sing05_1' 'V0=-1.770,Vy=-3.540,Oxy=1.022,mz_simed=' num2str(mz_simed) Center_Point '_Center' 'Experiment_Flat_rowstart' num2str(Row_Start) '.png'])
    end
end

for detuning_measure_index = [1:length(detuning_measure_list)]
    detuning_measure = detuning_measure_list(detuning_measure_index);
    qy_measure = qy_measure_list(detuning_measure_index);
    Row = Row_Start+detuning_measure_index-1;
    if Lower_Switch == 1
      load(sprintf("%s\\Rb87Data\\3DSOC_spin_texture\\Selected_saved\\ClassifyMeta_Row=%d,Detuning=%.3f_Lower.mat", Puter_Name_Pre, Row, detuning_measure), 'SP_Lower_Ave')
      SP_Ave = SP_Lower_Ave;
    else
      load(sprintf("%s\\Rb87Data\\3DSOC_spin_texture\\Selected_saved\\ClassifyMeta_Row=%d,Detuning=%.3f.mat", Puter_Name_Pre, Row, detuning_measure), 'SP_Ave')
    end
    FBZ = size(SP_Ave);
    if FBZ(1) == 1
        continue
    end
    if M_Center_Switch == 1
        SP_Ave = [SP_Ave(ceil(FBZ(1)/2):end-1,:);SP_Ave(1:ceil(FBZ(1)/2),:)];
        SP_Ave = [SP_Ave(:, ceil(FBZ(2)/2):end-1),SP_Ave(:,1:ceil(FBZ(2)/2))];
    end
    figure(3+detuning_measure_index)
    imagesc(SP_Ave, clims)
    tth = title(sprintf('$\\delta=%.3f$', detuning_measure));
    tth.Interpreter = 'latex';
    if 0
        tth = title("$q_z=0$");tth.Interpreter = 'latex'; tth.FontSize = 30;
        print('C:\Users\cheng\Desktop\2STdelta=-0.10352.pdf','-dpdf','-r0');
    end
    axis equal
    axis off
    colormap(cmap);
    if strcmp(Puter_Name, 'CALCENTER2')
        savefig(gcf, ['D:\Rb87figures\3DSOC_spin_texture\figures\Stack_New\Sing' 'V0=-1.770,Vy=-3.540,Oxy=1.022,mz_simed=' num2str(mz_simed) Center_Point '_Center' 'Experiment_Sing_rowstart' num2str(Row_Start) 'detuning=' num2str(detuning_measure) '.fig'], 'compact')
        saveas(gcf, ['D:\Rb87figures\3DSOC_spin_texture\figures\Stack_New\Sing' 'V0=-1.770,Vy=-3.540,Oxy=1.022,mz_simed=' num2str(mz_simed) Center_Point '_Center' 'Experiment_Sing_rowstart' num2str(Row_Start) 'detuning=' num2str(detuning_measure) '.png'])
    elseif strcmp(Puter_Name, 'VICTORCHENGPC')
        savefig(gcf, ['C:\Users\cheng\Desktop\pht\Sing05_1' 'V0=-1.770,Vy=-3.540,Oxy=1.022,mz_simed=' num2str(mz_simed) Center_Point '_Center' 'Experiment_Sing_rowstart' num2str(Row_Start) 'detuning=' num2str(detuning_measure) '.fig'], 'compact')
        saveas(gcf, ['C:\Users\cheng\Desktop\pht\Sing05_1' 'V0=-1.770,Vy=-3.540,Oxy=1.022,mz_simed=' num2str(mz_simed) Center_Point '_Center' 'Experiment_Sing_rowstart' num2str(Row_Start) 'detuning=' num2str(detuning_measure) '.png'])
    end
end
close all;